import {clearKey, clearLocalStorage, getObj, setObj, setStr} from './cacheUtil';
import {EnumUtil} from './enumUtil';

/**
 * 获取tokenValue
 */
export const getTokenValue = (): string | null => {
  const token = getObj(EnumUtil.TokenKey);
  if (token) {
    return token.tokenValue;
  }
  return null;
};
/**
 * 是否存在token
 */
export const isToken = (): boolean => {
  return !!getTokenValue();
}

/**
 * 存储token
 */
export const setToken = (token: TokenInfo) => {
  setObj(EnumUtil.TokenKey, token);
}
/**
 * 获取token
 */
export const getToken = (): TokenInfo => {
  return getObj(EnumUtil.TokenKey);
}
/**
 * 清除token
 */
export const clearToken = () => {
  clearLocalStorage()
}

/**
 * 存储登录设备
 */
export const setLoginDevice = (device: string) => {
  setStr(EnumUtil.LoginDevice, device);
}

/**
 * token信息结构
 */
export interface TokenInfo {
  tokenName: string;
  tokenValue: string;
  isLogin: boolean;
  loginId: string;
  loginType: string;
  tokenTimeout: number;
  sessionTimeout: number;
  tokenSessionTimeout: number;
  tokenActiveTimeout: number;
  /**
   * 登录设备
   */
  loginDevice: string;
  tag: string;
}
